//
//  OPNoteDetailCell.m
//  OnePiece
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPNoteDetailCell.h"

@implementation OPNoteDetailCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        
    }
    return self;
}


- (void)configureDataWithModel:(OPNoteDetailModel *)m
{
    [self.words removeFromSuperview];
    [self.whiteView removeFromSuperview];
    [self.coverImg removeFromSuperview];
    [self.movieVC.view removeFromSuperview];
    [self.web removeFromSuperview];
    
    if (m.video_file_640.length == 0) {
        if (m.picfile.length > 0) {
            _coverImg = [[UIImageView alloc]init];
            self.coverImg.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, m.pich * (SCREEN_WIDTH - 20) / m.picw);
            [self.coverImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.117go.com/timg/f640/%@",m.picfile]]placeholderImage:[UIImage imageNamed:@"beijingImg.jpg"]];
            [self.contentView addSubview:_coverImg];
        }
    }
    else
    {
        _movieVC = [[AVPlayerViewController alloc]init];
        _movieVC.player = [AVPlayer playerWithURL:[NSURL URLWithString:m.video_file_640]];
        _movieVC.view.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, m.pich * (SCREEN_WIDTH - 20) / m.picw - 100);
        [self.contentView addSubview:_movieVC.view];
    }

    
    if (m.words.length > 0) {
        self.words = [[UILabel alloc]init];
        self.whiteView = [[UIView alloc]init];
        _whiteView.backgroundColor = [UIColor whiteColor];
        if (m.picfile.length > 0)
        {
            if (m.video_file_640.length == 0) {
                _words.frame = CGRectMake(20, _coverImg.frame.size.height + 20, SCREEN_WIDTH - 40, [OPNoteDetailCell heightForString:m.words]);
                _whiteView.frame = CGRectMake(10, _coverImg.frame.size.height + 10, SCREEN_WIDTH - 20, [OPNoteDetailCell heightForString:m.words] + 20);
            }
            else
            {
                _words.frame = CGRectMake(20, _movieVC.view.frame.size.height + 20, SCREEN_WIDTH - 40, [OPNoteDetailCell heightForString:m.words]);
                _whiteView.frame = CGRectMake(10, _movieVC.view.frame.size.height + 10, SCREEN_WIDTH - 20, [OPNoteDetailCell heightForString:m.words] + 20);
            }
        }
        else
        {
            _words.frame = CGRectMake(20, 20, SCREEN_WIDTH - 40, [OPNoteDetailCell heightForString:m.words]);
            _whiteView.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, [OPNoteDetailCell heightForString:m.words] + 20);
        }
        _words.font = [UIFont systemFontOfSize:14];
        _words.numberOfLines = 0;

        [self.contentView addSubview:_whiteView];
        [self.contentView addSubview:_words];
        self.words.text = m.words;
    }
    
}

+ (CGFloat)heightForString:(NSString *)string
{
    if (string.length > 0) {
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
        CGRect bound = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
        return bound.size.height;
    }
    else
    {
        return 0;
    }

}

+ (CGFloat)heightForModel:(OPNoteDetailModel *)m
{
    if (m.words.length > 0 && m.picfile.length > 0) {
        if (m.video_file_640.length > 0) {
            return m.pich * (SCREEN_WIDTH - 20) / m.picw - 60 + [OPNoteDetailCell heightForString:m.words];
        }
        else
        {
            return m.pich * (SCREEN_WIDTH - 20) / m.picw + 40 + [OPNoteDetailCell heightForString:m.words];
        }

    }
    else if(m.picfile.length > 0 && m.words.length == 0)
    {
        if (m.video_file_640.length > 0) {
            return m.pich * (SCREEN_WIDTH - 20) / m.picw - 30;
        }
        else
        {
            return m.pich * (SCREEN_WIDTH - 20) / m.picw + 20;
        }
    }
    else
    {
        return 40 + [OPNoteDetailCell heightForString:m.words];
    }

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
